package com.zhihuichongcheng.application.ui.task.adapter.holder;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.task.FilterBean;
import com.zhihuichongcheng.application.ui.task.adapter.FilterAdapter;

/**
 * filter Created by LK on 2017/1/6.
 */
public class FilterHolder extends RecyclerView.ViewHolder {
    private TextView tvName;
    private CheckBox imSelector;

    public FilterHolder(View itemView) {
        super(itemView);
        tvName = (TextView) itemView.findViewById(R.id.tv_name);
        imSelector = (CheckBox) itemView.findViewById(R.id.im_switch);
    }

    public void setData(final FilterBean filterBean, final FilterAdapter adapter, final int position) {
        tvName.setText(filterBean.getName());
        imSelector.setChecked(filterBean.isSelected());
        if (filterBean.getType() == FilterBean.FilterType.Single) {
            imSelector.setButtonDrawable(filterBean.isSelected() ?
                    R.drawable.ic_check_select : R.drawable.ic_check_normal);
        } else {
            imSelector.setButtonDrawable(filterBean.isSelected() ?
                    R.drawable.ic_switch_select : R.drawable.ic_switch_normal);
        }

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick(filterBean, adapter);
            }
        });

        imSelector.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick(filterBean, adapter);
            }
        });

    }

    private void onItemClick(FilterBean filterBean, FilterAdapter adapter) {
        if (filterBean.getType() == FilterBean.FilterType.Single)
            adapter.notifySingleItemSelected(filterBean);
        else
            filterBean.setSelected(!filterBean.isSelected());
        adapter.notifyDataSetChanged();
    }

}
